// pages/coin/uploadImage/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    img_url: '',
    title: '',
    content: '',
    popupShow: false,
    getType: false,
    typeName: '',
    typeId: '',
    typeNames: ['不使用一次性餐具', '捐赠、回收衣物', '使用共享单车', '乘坐公共交通工具', '其他类型，请等待管理员审核'],
    types: [
      {
        id: 1,
        typeName: '不使用一次性餐具'
      },
      {
        id: 2,
        typeName: '捐赠、回收衣物'
      },
      {
        id: 4,
        typeName: '使用共享单车'
      },
    ]
  },

  /**
 * 获取标题输入
 */
  titleInput: function (e) {
    this.setData({
      title: e.detail.value
    })
  },

  /**
   * 获取输入
   */
  input: function (e) {
    this.setData({
      content: e.detail.value
    })
  },

  /**
   * 点击发布
   */
  send: function () {
    var that = this;

    if (!that.data.getType || that.data.content == '' || that.data.title == '') {
      wx.showToast({
        title: '请完善信息！',
        icon: 'error',
        duration: 2000
      })
    } else {

      wx.showLoading({
        title: '上传中',
      })

      wx.uploadFile({
        url:  app.globalData.url + '/plog_post/',
        filePath: that.data.img_url,
        name: 'file',
        formData: {
          userID: getApp().globalData.user.userId,
          plogTypeID: that.data.typeId,
          imagePath: that.data.img_url,
          plogName: that.data.title,
          plogContent: that.data.content
        },
        success (res){
          var json = JSON.parse(res.data)
          console.log(json)

          wx.hideLoading()

          if (res.statusCode == 200) {
            wx.navigateTo({
              url: '../managePost/index?message=' + json.message
            })
          } else {
            wx.showToast({
              title: '发布失败',
              icon: 'success',
              duration: 2000
            })
          }
        },
        fail: function (res) {
          wx.hideLoading()

          wx.showToast({
            title: '连接失败',
            icon: 'error',
            duration: 2000
          })
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;

    wx.getStorage({
      key: 'imageUrl',
      success: function (res) {
        console.log(res.data)
        that.setData({
          img_url: res.data[0]
        })
      }
    })

    wx.request({
      url: app.globalData.url + "/plog_type",
      header: { 'content-type': 'application/json' },
      method: 'GET',
      success: (res) => {
        console.log(res.data)
        if (res.statusCode != 200) {
          wx.showToast({
            title: '获取失败',
            icon: 'error',
            duration: 2000
          })
        } else {
          var names = new Array(res.data)
          for (var i = 0; i < res.data.length; i++) {
            names[i] = res.data[i].typeName
          }

          that.setData({
            types: res.data,
            typeNames: names
          })
        }
      },
      fail: function (res) {
        wx.showToast({
          title: '连接失败',
          icon: 'error',
          duration: 2000
        })
      }
    });
  },

  /**
   * 显示选择器
   */
  showPicker() {
    var that = this

    that.setData({
      popupShow: true
    })
  },

  /**
   * 选择类型
   */
  popupConfirm(event) {
    console.log(event.detail)
    var that = this
    var typeIndex = event.detail.index
    var typeId = that.data.types[typeIndex].id

    that.setData({
      popupShow: false,
      typeName: event.detail.value,
      typeId: typeId,
      getType: true
    })
  },

  /**
   * 取消
   */
  popupCancel(event) {
    var that = this

    that.setData({
      popupShow: false
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})